/*
 * @Author: xiaosihan 
 * @Date: 2024-01-08 21:46:30 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-03-17 20:59:27
 */

import { configure, observable, toJS } from "mobx";
import config from "./config/config";
import adPNG from "./img/ad.png";
import { BuildData, BuildWindowData, Project, WindowData, WindowTypeData } from "./type";
import windowApi from "@service/windowApi";
import utils from "@utils";
import { get } from "lodash";

configure({ enforceActions: "never" });

const homeStore = window.homeStore = observable({

    // 所有项目
    projects: [] as Array<Project>,
    setProjects(projects: Array<Project>) {
        this.projects = projects;
    },

    // 项目编号
    projecId: "000000",
    // 项目名称
    projectName: "新疆中航投资有限公司",
    // 责任人
    personResponsible: "xxx",
    // 项目编号
    projectCode: "",
    //模型名称
    MdTitle: "....",

    // 当前选中的项目id
    activeProjectId: "",
    async setActiveProjectId(activeProjectId: string) {
        this.activeProjectId = activeProjectId;
        if (activeProjectId) {
            // 请求当前项目的楼层数据
            const builds = await windowApi.getBuildDatas(activeProjectId);
            this.setBuilds(builds.data.data);

            const data = this.projects.find(d => d._AutoID === activeProjectId);
            if (data) {
                this.projecId = activeProjectId;
                this.projectName = data.ProjectName;
                this.projectCode = data.ProjectCode;
                this.personResponsible = data.RespPerson;
                this.MdTitle = data.MdTitle || '....';
            }

        }
    },

    //当前项目的楼栋数据
    builds: [] as Array<BuildData>,
    setBuilds(builds: Array<BuildData>) {
        this.builds = builds;
        if (builds[0]) {
            this.setActiveBuildId(builds[0].buildId);
        }
    },

    //当前选中的楼栋id
    activeBuildId: "",
    setActiveBuildId(activeBuildId: string) {
        this.activeBuildId = activeBuildId;
    },

    // 状态  分布|安装
    state: "distribution" as "distribution" | "install",
    setState(state: "distribution" | "install") {
        this.state = state;
        this.activeInstall = []; // 清空选中的安装状态展示
        this.activeType = []; // 清空选中的窗户类型展示
    },

    //显示状态
    showState: false,
    setShowState(showState: boolean) {
        this.showState = showState;
    },

    // 当前选中的楼栋 0,1,2
    build: 0,
    setBuild(build: number) {
        this.build = build;
    },

    //楼栋数据
    buildDatas: [
        {
            floorNum: 5, //楼层数
            windowNum: 5, // 每面墙窗户的数量
            windowDatas: new Array(5 * 5 * 4).fill(0).map((v, i) => {
                return config.randWindowData((i % (5 * 4)) > (5 * 2 - 1) ? 0 : 1);
            })   // 窗户的数据
        },
        {
            floorNum: 5, //楼层数
            windowNum: 5, // 每面墙窗户的数量
            windowDatas: new Array(5 * 5 * 4).fill(0).map((v, i) => {
                return config.randWindowData((i % (5 * 4)) > (5 * 2 - 1) ? 0 : 1);
            })   // 窗户的数据
        },
        {
            floorNum: 5, //楼层数
            windowNum: 5, // 每面墙窗户的数量
            windowDatas: new Array(5 * 5 * 4).fill(0).map((v, i) => {
                return config.randWindowData((i % (5 * 4)) > (5 * 2 - 1) ? 0 : 1);
            })   // 窗户的数据
        },
    ] as Array<{ floorNum: number, windowNum: number, windowDatas: Array<WindowData> }>,

    // 设置楼层数
    setFloorNum(floorNum: number) {
        this.buildDatas[this.build].floorNum = floorNum;
        const { windowNum } = this.buildDatas[this.build];
        const dataLength = this.buildDatas[this.build].windowDatas.length;

        // 如果增加了玻璃就增加玻璃数据
        if (floorNum * windowNum * 4 > dataLength) {
            this.buildDatas[this.build].windowDatas = [
                ...this.buildDatas[this.build].windowDatas,
                ...new Array((floorNum * windowNum * 4) - dataLength).fill(0).map((v, i) => {
                    return config.randWindowData((i % (windowNum * 4)) > (windowNum * 2 - 1) ? 0 : 1);
                })
            ];
        } else {
            this.buildDatas[this.build].windowDatas.length = (floorNum * windowNum * 4);
        }

        this.buildDatas = [...this.buildDatas];
    },

    // 每面墙窗户的数量
    setWindowNum(windowNum: number) {
        this.buildDatas[this.build].windowNum = windowNum;
        const { floorNum } = this.buildDatas[this.build];

        // 如果增加了玻璃就增加玻璃数据
        this.buildDatas[this.build].windowDatas = [
            ...new Array((floorNum * windowNum * 4)).fill(0).map((v, i) => {
                return config.randWindowData((i % (windowNum * 4)) > (windowNum * 2 - 1) ? 0 : 1);
            })
        ];

        this.buildDatas = [...this.buildDatas];
    },

    // 1楼的高度
    oneFloorHeight: 0,
    setOneFloorHeight(oneFloorHeight: number) {
        this.oneFloorHeight = oneFloorHeight;
    },

    //每层楼房的高度
    floorHeight: 0.3,
    setFloorHeight(floorHeight: number) {
        this.floorHeight = floorHeight;
    },

    //窗户的颜色
    windowColor: "#ffffff",
    setWindowColor(windowColor: string) {
        this.windowColor = windowColor;
    },

    //广告
    advertisement: adPNG,
    setAdvertisement(advertisement: string) {
        this.advertisement = advertisement;
    },

    // 获取窗户安装转态
    buildData: [] as Array<BuildWindowData>,
    installs: ["框安装", "塞缝处理", "扇/五金", "玻璃", "密封胶"],
    setBuildData(buildData: any) {
        this.buildData = buildData;
        this.activeInstall = [];
    },

    //选中的安装状态
    activeInstall: [""],
    //全选安装状态
    checkAllInstallType() {
        if (this.activeInstall.length < this.installs.length) {
            this.activeInstall = toJS(this.installs);
        } else {
            this.activeInstall = [];
        }
    },
    //选择安装状态
    checkInstallType(type: string) {
        if (this.activeInstall.includes(type)) {
            this.activeInstall = this.activeInstall.filter(t => t !== type);
        } else {
            this.activeInstall = [...this.activeInstall, type];
        }
    },

    // 获取窗户类型列表
    windowType: [] as Array<WindowTypeData>,
    windowTypeNames: [] as Array<string>,
    setWindowType(windowType: Array<WindowTypeData>) {
        this.windowType = windowType;
        this.activeType = [];
        const windowTypeNames: Array<string> = [];
        windowType.map(w => {
            w.winList.map(l => {
                if (!windowTypeNames.includes(l.name) && !windowTypeNames.includes(l.name)) {
                    windowTypeNames.push(l.name);
                }
            })
        });
        this.windowTypeNames = windowTypeNames;
    },


    //选中的窗户类型
    activeType: [""],
    //全选窗户类型
    checkAllType() {
        if (this.activeType.length < this.windowTypeNames.length) {
            this.activeType = toJS(this.windowTypeNames);
        } else {
            this.activeType = [];
        }
    },
    //选择窗户类型
    checkType(type: string) {
        if (this.activeType.includes(type)) {
            this.activeType = this.activeType.filter(t => t !== type);
        } else {
            this.activeType = [...this.activeType, type];
        }
    }

}, {}, { deep: false });

(async () => {

    Promise.all([
        windowApi.getProjects(),// 获取所有项目
        windowApi.getInstanllStateColor()
    ]).then(([res1, res2]) => {
        const colorConfigs: Array<{ Key: string, Val: string }> = (get(res2, ["data", "data"]) || []);
        //保存安转状态的颜色配置
        config.installState.map(i => {
            const color = get(colorConfigs.find(c => c.Key === i.key), ["Val"]) || "#ffffff";
            i.installColor = color;
        });
        homeStore.setProjects(res1.data.data);
        const projectId = utils.getQueryString("projectid") as string;
        homeStore.setActiveProjectId(projectId);
    })

})();

export default homeStore;